package com.ssh.api.action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.json.JSONUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh.common.log.LogFactory;
import com.ssh.common.log.LogName;
import com.ssh.common.util.JsonResult;
import com.ssh.entity.ChangGui;
import com.ssh.entity.TaskProductEmployee;
import com.ssh.manager.TaskProductEmployeeManager;
@Controller
@ParentPackage("client")
@Scope("prototype")
@Results(value={@Result(name ="productList",type="json",params={"root","jr","excludeNullProperties","true"}),
		@Result(name ="addChangGui",type="json",params={"root","jr","includeProperties","data\\[\\d+\\]\\.id","excludeNullProperties","true"}),
		@Result(name ="addHelpSale",type="json",params={"root","jr","includeProperties","data\\[\\d+\\]\\.id","excludeNullProperties","true"}),
		@Result(name ="allProductByTaskEmployee",type="json",params={"root","jr","excludeProperties","data\\[\\d+\\]\\.startDate,data\\[\\d+\\]\\.endDate,data\\[\\d+\\]\\.createDate," +
				"data\\[\\d+\\]\\.del,data\\[\\d+\\]\\.employee\\.*,data\\[\\d+\\]\\.taskBrand\\.createDate,data\\[\\d+\\]\\.taskBrand\\.del,data\\[\\d+\\]\\.taskBrand\\.task\\.*," +
				"data\\[\\d+\\]\\.taskBrand\\.brand\\.content,data\\[\\d+\\]\\.taskBrand\\.brand\\.createDate,data\\[\\d+\\]\\.taskBrand\\.brand\\.del,data\\[\\d+\\]\\.taskBrand\\.brand\\.startDate," +
				"data\\[\\d+\\]\\.taskBrand\\.brand\\.endDate,data\\[\\d+\\]\\.taskBrand\\.endDate,data\\[\\d+\\]\\.taskBrand\\.startDate," +
				"data\\[\\d+\\]\\.superMarketProduct\\.createDate,data\\[\\d+\\]\\.superMarketProduct\\.del,data\\[\\d+\\]\\.superMarketProduct\\.product\\.brand\\.content,data\\[\\d+\\]\\.superMarketProduct\\.product\\.brand\\.createDate,data\\[\\d+\\]\\.superMarketProduct\\.product\\.brand\\.del," +
				"data\\[\\d+\\]\\.superMarketProduct\\.product\\.createDate,data\\[\\d+\\]\\.superMarketProduct\\.product\\.del," +
				"data\\[\\d+\\]\\.superMarketProduct\\.superMarket\\.company\\.createDate,data\\[\\d+\\]\\.superMarketProduct\\.superMarket\\.company\\.conetent,data\\[\\d+\\]\\.superMarketProduct\\.superMarket\\.company\\.del,data\\[\\d+\\]\\.superMarketProduct\\.superMarket\\.company\\.content," +
				"data\\[\\d+\\]\\.superMarketProduct\\.superMarket\\.createDate,data\\[\\d+\\]\\.superMarketProduct\\.superMarket\\.conetent,data\\[\\d+\\]\\.superMarketProduct\\.superMarket\\.del,data\\[\\d+\\]\\.superMarketProduct\\.superMarket\\.content,data\\[\\d+\\]\\.superMarketProduct\\.superMarket\\.tel"})
})

public class ProductClientAction extends ActionSupport {
	private static final long serialVersionUID = 970773022382019911L;
	JsonResult jr=new JsonResult();
	private Long superMarketId;
	private Long employeeId;
	private Long brandId;
	private Long taskId;
	private String params;

	@Resource(name="taskProductEmployeeManagerImpl")
	private TaskProductEmployeeManager taskProductEmployeeManagerImpl;
	
	public  String productList(){
		jr.setMethod("productList");
		try{
	    	jr.setCode("0");
	    	List<Object> listresult=taskProductEmployeeManagerImpl.productlistOfSuperMarket(taskId, superMarketId,brandId, employeeId);
	    	jr.setData(listresult);
		}catch (Exception e) {
			jr.setCode("-1");
			jr.setMessage(e.getMessage());
			LogFactory.getDefaultLog().error("查询失败", LogName.API);
		}
		return "productList";
    }
	
	public String addChangGui(){
		jr.setMethod("addChangGui");
		try {
			ArrayList<Map<String, String>> json = (ArrayList<Map<String, String>>)JSONUtil.deserialize(params);
			 List<ChangGui> list = new ArrayList<ChangGui>();   
		        for (int i = 0; i < json.size(); i++) {   
                 ChangGui changGui=new ChangGui();
		         changGui.setCreateDate( new Timestamp(System.currentTimeMillis()));
		         changGui.setDel(0);
		         changGui.setFenxiao(json.get(i).get("fenxiao")); 
		         changGui.setStock(Integer.parseInt(json.get(i).get("stock")));
		         changGui.setPrice(json.get(i).get("price"));
		         changGui.setChenlie(json.get(i).get("chenlie"));
		         changGui.setTaskProductEmployee(taskProductEmployeeManagerImpl.findTaskProductEmployeeEmployee(Long.parseLong(json.get(i).get("taskProductEmployeeId"))));
                 list.add(changGui);
		        }  
		        List<ChangGui> lists=taskProductEmployeeManagerImpl.addChangGui(list);
		       if(lists!=null&&lists.size()>=0){
		    	   jr.setCode("0");
		    	   jr.setData(lists);
		       }else{
		    	   jr.setCode("-1");
		    	   jr.setMessage("保存失败");
		       }

		} catch (Exception e) {
			LogFactory.getDefaultLog().error(e.getLocalizedMessage(), LogName.API);
			 jr.setCode("-1");
			 jr.setMessage(e.getMessage());
		}
		
		return "addChangGui";
	}


	
	public String addHelpSale(){
		jr.setMethod("addHelpSale");
		try {
			ArrayList<Map<String, String>> param = (ArrayList<Map<String, String>>)JSONUtil.deserialize(params);
		        List<Object> lists=taskProductEmployeeManagerImpl.addHelpSale(param);
		       if(lists!=null&&lists.size()>=0){
		    	   jr.setCode("0");
		    	   jr.setData(lists);
		       }else{
		    	   jr.setCode("-1");
		    	   jr.setMessage("保存失败");
		       }

		} catch (Exception e) {
			LogFactory.getDefaultLog().error(e.getLocalizedMessage(), LogName.API);
			 jr.setCode("-1");
			 jr.setMessage(e.getMessage());
		}
		
		return "addHelpSale";
	}

	public String allProductByTaskEmployee(){
		
		jr.setMethod("allProductByTaskEmployee");
		try {
		        List<TaskProductEmployee> lists=taskProductEmployeeManagerImpl.findTaskProductEmployeeByTaskEmployee(taskId, employeeId);
		       if(lists!=null&&lists.size()>=0){
		    	   jr.setCode("0");
		    	   jr.setData(lists);
		       }else{
		    	   jr.setCode("-1");
		    	   jr.setMessage("保存失败");
		       }

		} catch (Exception e) {
			LogFactory.getDefaultLog().error(e.getLocalizedMessage(), LogName.API);
			 jr.setCode("-1");
			 jr.setMessage(e.getMessage());
		}
		
		return "allProductByTaskEmployee";
	}

	public JsonResult getJr() {
		return jr;
	}

	public void setJr(JsonResult jr) {
		this.jr = jr;
	}



	public void setSuperMarketId(Long superMarketId) {
		this.superMarketId = superMarketId;
	}



	public void setEmployeeId(Long employeeId) {
		this.employeeId = employeeId;
	}



	public void setBrandId(Long brandId) {
		this.brandId = brandId;
	}



	public void setTaskId(Long taskId) {
		this.taskId = taskId;
	}

	public String getParams() {
		return params;
	}

	public void setParams(String params) {
		this.params = params;
	}

	


}
